package com.shuoyue.ycgk.ui.recruitment.resume.major;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.entity.MajorSection;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;

import java.util.List;

import butterknife.BindView;

public class FragmentMajorList extends BaseMvpFragment<MajorContract.Presenter>
        implements MajorContract.View {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshlayout;


    MajorSectionAdapter adapter;

    public static FragmentMajorList getInstance(String edu) {

        FragmentMajorList fragmentMajorList = new FragmentMajorList();
        fragmentMajorList.edu = edu;
        Bundle bundle = new Bundle();
        bundle.putString("edu", edu);
        fragmentMajorList.setArguments(bundle);
        return fragmentMajorList;
    }

    String edu;

    @Override
    public void initParams(Bundle bundle) {
        edu = bundle.getString("edu");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_sigle_list;
    }

    @Override
    protected void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 12), false));
        adapter = new MajorSectionAdapter(null, name -> {
            mActivity.setResult(Activity.RESULT_OK, new Intent().putExtra("name", name));
            mActivity.finish();
        });
        adapter.setOnItemChildClickListener((adapter1, view, position) -> {
            MajorSection type = (MajorSection) adapter1.getItem(position);
            type.setSelect(!type.isSelect());
            adapter1.notifyDataSetChanged();
        });
        recyclerView.setAdapter(adapter);

        refreshlayout.setEnableLoadMore(false);
        refreshlayout.setOnRefreshListener(refreshLayout -> {

            mPresenter.getMajor(edu, null);
            refreshlayout.finishRefresh();
        });
        registEmptyView(adapter);
    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter = new MajorContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getMajor(edu, null);
    }

    @Override
    public String getTitle() {
        return edu;
    }

    @Override
    public void getMajorSuc(List<MajorSection> mData) {
        adapter.resetData(mData);
        Log.e("type", "----------------------" + edu + "--------------------------------------");

        Log.e("adapterCount", adapter.getItemCount() + "");
        Log.e("mData", mData + "");
        Log.e("adapterDate", adapter.getData() + "");

    }

    public void search(String title) {
        mPresenter.getMajor(edu, title);
    }
}
